<script setup>
import { ref } from 'vue'
import { updateMyPassword } from '@/api/MyselfApi'

// 用来装dom的盒子
const oldPassword = ref('')
const newPassword = ref('')
const isNewPassword = ref('')
const oldPasswordCheck = ref('')
const newPasswordCheck = ref('')
const isNewPasswordCheck = ref('')
const isShowOldPasswordCheck = ref(false)
const isShowNewPasswordCheck = ref(false)
const isShowIsNewPasswordCheck = ref(false)

// 封装校验函数
const updateCheck = (name,string) => {
  if (name === 'oldPassword'){
    // 给旧密码校验
    oldPasswordCheck.value.innerText = string
    isShowOldPasswordCheck.value = true
    setTimeout(() => {
      oldPasswordCheck.value.innerText = ''
    isShowOldPasswordCheck.value = false
    },5000)
  } else if(name === 'newPassword'){
    // 给密码校验
    newPasswordCheck.value.innerText = string
    isShowNewPasswordCheck.value = true
    setTimeout(() => {
      newPasswordCheck.value.innerText = ''
      isShowNewPasswordCheck.value = false
    },5000)
  } else if(name === 'isNewPassword'){
    // 给新密码校验
    isNewPasswordCheck.value.innerText = string
    isShowIsNewPasswordCheck.value = true
    setTimeout(() => {
      isNewPasswordCheck.value.innerText = ''
      isShowIsNewPasswordCheck.value = false
    },5000)
  }
}

// 提交
const submit = async () => {
  // console.log('43',oldPassword.value.value)
  if (oldPassword.value.value === ''){
    updateCheck('oldPassword','旧密码不能为空')
    return
  }else if (newPassword.value.value === ''){
    updateCheck('newPassword','新密码不能为空')
    return
  }else if (isNewPassword.value.value === ''){
    updateCheck('isNewPassword','确认密码不能为空')
    return
  }else if (!/^[a-zA-Z0-9]{6,15}$/.test(oldPassword.value.value)){
    updateCheck('oldPassword','密码格式错误')
    return
  }else if (!/^[a-zA-Z0-9]{6,15}$/.test(newPassword.value.value)){
    updateCheck('newPassword','密码格式错误')
    return
  }else if (!/^[a-zA-Z0-9]{6,15}$/.test(isNewPassword.value.value)){
    updateCheck('isNewPassword','密码格式错误')
    return
  }else if (isNewPassword.value.value !== newPassword.value.value){
    updateCheck('isNewPassword','密码不一致')
    return
  }else if (oldPassword.value.value === newPassword.value.value){
    updateCheck('isNewPassword','输入的新密码与输入的原密码相同')
    return
  }
  await updateMyPassword(oldPassword.value.value,newPassword.value.value,newPassword.value.value)
  .then((res)=>{
    console.log('res',res)
    oldPassword.value.value = ''
    newPassword.value.value = ''
    isNewPassword.value.value = ''
    // history.go(0)
  })
  .catch((err)=>{
    console.log('err',err)
    alert('密码错误')
    return
  })
  alert('修改成功')
  // oldPassword.value.value = ''
  // newPassword.value.value = ''
  // isNewPassword.value.value = ''
  // history.go(0)
}

// 重置
const reset = () => {
  oldPassword.value.value = ''
  newPassword.value.value = ''
  isNewPassword.value.value = ''
}
</script>

<template>
  <div class="maxBox">
    <div class="top">
      <div class="title">重置密码</div>
    </div>
    <div class="foot">
      <div class="inps">
        <span>原密码</span>
        <input type="text" class="inp"  value="" ref='oldPassword'>
        <div class="jiaoYan" ref='oldPasswordCheck' v-show='isShowOldPasswordCheck'>我是校验框</div>
      </div>
      <div class="inps">
        <span>新密码</span>
        <input type="text" class="inp" ref='newPassword'>
        <div class="jiaoYan" ref='newPasswordCheck' v-show='isShowNewPasswordCheck'>我是校验框</div>
      </div>
      <div class="inps">
        <span>确认新密码</span>
        <input type="text" class="inp" ref="isNewPassword">
        <div class="jiaoYan" ref='isNewPasswordCheck' v-show='isShowIsNewPasswordCheck'>我是校验框</div>
      </div>
      <div class="lastBtns">
        <button class="btn tiJiao" @click='submit'>修改密码</button>
        <button class="minBtn chongZhi" @click="reset">重置</button>
      </div>
    </div>
  </div>

</template>

<style scoped>
  * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    /* border: 1px solid red; */
  }
  .maxBox {
    width: 980px;
    min-height: 1000px;
    /* background-color: rgb(255, 255, 255); */
    /* box-shadow: -2px -2px 5px rgb(210, 208, 208); */
  }
  /* top样式开始 */
  .top {
    width: 100%;
    height: 80px;
    /* position: relative; */
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .title {
    width: 200px;
    height: 100%;
    text-align: center;
    line-height: 80px;
    font-weight: 700;
    color: black;
    font-size: 40px;
  }
  /* top样式结束 */
  .foot {
    width: 100%;
  }
  /* 输入框加文本加校验框开始 */
  .inps {
    height: 80px;
    width: 500px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }
  .inp {
    width: 350px;
    height: 35px;
    border-radius: 5px;
    background-color: transparent;
  }
  .inp:focus {
    outline: none; /* 移除默认的聚焦轮廓 */
    border-color: #74b4e9; /* 设置蓝色边框 */
    transition: all 0.3s ease; /* 添加过渡动画 */
  }
  .inps span {
    display: block;
    width: 100px;
    height: 100%;
    text-align: center;
    line-height: 80px;
    font-size: 18px;
  }
  .jiaoYan {
    width: 200px;
    height: 20px;
    /* background-color: aqua; */
    overflow: hidden;
    position: absolute;
    bottom: 1px;
    left: 125px;
    text-align: 20px;
    font-size: 10px;
    color: red;
  }
  /* 输入框加文本加校验框结束 */
  .tiJiao {
    margin-left: 160px;
  }
  .chongZhi {
    margin-left: 20px;
  }
  .lastBtns {
    height: 50px;
    display: flex;
  }
</style>